﻿using System;
using System.Collections.Generic;
using System.Linq;
using WindowsForms.AOnline.Model.AccountingServiceReference;
using WindowsForms.AOnline.Model.BusinessObjects.Inventory;
using WindowsForms.AOnline.Model.BusinessObjects.Report.Stock;

namespace WindowsForms.AOnline.Model.DataTransferObjectMapper.Voucher  
{
    /// <summary>
    /// 
    /// </summary>
    public static class Mapper
    {
        #region ToDataTransferObjects

        /// <summary>to
        /// To the data transfer objects.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static IList<ItemTransactionDto> ToDataTransferObjects(IEnumerable<ItemTransactionModel> c)
        {
            return c == null ? null : c.Select(source => ToDataTransferObject((source))).ToList();
        }

        /// <summary>to
        /// To the data transfer objects.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static IList<ItemTransactionDetailDto> ToDataTransferObjects(IEnumerable<ItemTransactionDetailModel> c)
        {
            return c == null ? null : c.Select(source => ToDataTransferObject((source))).ToList();
        }

        public static IList<StockBacklogDto> ToDataTransferObjects(IEnumerable<StockBacklogModel> c)
        {
            return c == null ? null : c.Select(source => ToDataTransferObject((source))).ToList();
        }
        #endregion

        #region ToDataTransferObject.

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static ItemTransactionDto ToDataTransferObject(ItemTransactionModel c)
        {
            if (c == null) return null;

            return new ItemTransactionDto
                {
                    
                    RefId = c.RefId,
                    RefTypeId = c.RefTypeId,
                    RefNo = c.RefNo,
                    RefDate = DateTime.Parse(c.RefDate),
                    UnitId = c.UnitId,
                    TaxCode = c.TaxCode,
                    CustomerId = c.CustomerId,
                    VendorId = c.VendorId,
                    TotalAmountTax = c.TotalAmountTax,
                    Trader = c.Trader,
                    StockId = c.StockId,
                    TotalAmountOc = c.TotalAmountOc,
                    JournalMemo = c.JournalMemo,
                    DocumentInclude = c.DocumentInclude,
                    AccountingObjectType = c.AccountingObjectType,
                    IsCalculatePrice = c.IsCalculatePrice,
               //     ItemTransactionDetails = (ItemTransactionDetailDto[]) (c.ItemTransactionDetails == null ? null : ToDataTransferObjects(c.ItemTransactionDetails))
                };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static ItemTransactionDetailDto[] ToDataTransferObject(IList<ItemTransactionDetailModel> c)
        {
            if (c == null) return null;
            ItemTransactionDetailDto[] inwardOutwardDetails = new ItemTransactionDetailDto[c.Count];
            int i = 0;

            foreach (var itemTransactionDetailDto in c)
            {
                inwardOutwardDetails[i] = new ItemTransactionDetailDto
                    {
                        ItemTransactionDetailId = itemTransactionDetailDto.ItemTransactionDetailId,
                        InventoryItemId = itemTransactionDetailDto.InventoryItemId,
                        AccountNumber = itemTransactionDetailDto.AccountNumber,
                        CorrespondingAccountNumber = itemTransactionDetailDto.CorrespondingAccountNumber,
                        Description = itemTransactionDetailDto.Description,
                        AmountOc = itemTransactionDetailDto.AmountOc,
                        AmountVat = itemTransactionDetailDto.AmountVat,
                        AccountNumberVat = itemTransactionDetailDto.AccountNumberVat,
                        TaxRate = itemTransactionDetailDto.TaxRate,
                        RefId = itemTransactionDetailDto.RefId,
                        Quantity = itemTransactionDetailDto.Quantity,
                        Price = itemTransactionDetailDto.Price,
                        VoucherTypeId = itemTransactionDetailDto.VoucherTypeId,
                    };
                i++;
            }

            return inwardOutwardDetails;
           
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static ItemTransactionDetailDto ToDataTransferObject(ItemTransactionDetailModel c)
        {
            if (c == null) return null;
            return new ItemTransactionDetailDto
                {
                    ItemTransactionDetailId = c.ItemTransactionDetailId,
                    InventoryItemId = c.InventoryItemId,
                    AccountNumber = c.AccountNumber,
                    CorrespondingAccountNumber = c.CorrespondingAccountNumber,
                    Description = c.Description,
                    AmountOc = c.AmountOc,
                    AmountVat = c.AmountVat,
                    AccountNumberVat = c.AccountNumberVat,
                    TaxRate = c.TaxRate,
                    RefId = c.RefId,
                    Quantity = c.Quantity,
                    Price = c.Price,
                    VoucherTypeId = c.VoucherTypeId,
                };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static StockBacklogDto ToDataTransferObject(StockBacklogModel c)
        {
            if (c == null) return null;
            return new StockBacklogDto
            {
                InventoryItemCode = c.InventoryItemCode,
                InventoryItemName = c.InventoryItemName,
                Unit = c.Unit,
                QuantityOpening = c.QuantityOpening,
                QuantityInwardStock = c.QuantityInwardStock,
                QuantityOutwardStock = c.QuantityOutwardStock,
                QuantityClosing = c.QuantityClosing,

                OrgPriceClosing = c.OrgPriceClosing,
                OrgPriceInwardStock = c.OrgPriceInwardStock,
                OrgPriceOpening = c.OrgPriceOpening,
                OrgPriceOutwardStock = c.OrgPriceOutwardStock,
            };
        }

        #endregion

        #region FromDataTransferObject

        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static ItemTransactionModel FromDataTransferObject(ItemTransactionDto c, ItemTransactionDetailDto[] details)
        {
            if (c == null) return new ItemTransactionModel
                {
                    ItemTransactionDetails = FromDataTransferObjects(details)
                };
            return new ItemTransactionModel
                {
                    RefId = c.RefId,
                    RefTypeId = c.RefTypeId,
                    RefNo = c.RefNo,
                    RefDate = c.RefDate.ToShortDateString(),
                    UnitId = c.UnitId,
                    TaxCode = c.TaxCode,
                    CustomerId = c.CustomerId,
                    VendorId = c.VendorId,
                    TotalAmountTax = c.TotalAmountTax,
                    Trader = c.Trader,
                    StockId = c.StockId,
                    TotalAmountOc = c.TotalAmountOc,
                    JournalMemo = c.JournalMemo,
                    DocumentInclude = c.DocumentInclude,
                    AccountingObjectType = c.AccountingObjectType,
                    IsCalculatePrice = c.IsCalculatePrice,
                    ItemTransactionDetails = FromDataTransferObjects(details)
                };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static ItemTransactionModel FromDataTransferObject(ItemTransactionDto c)
        {
            if (c == null) return null;
            return new ItemTransactionModel
            {
                RefId = c.RefId,
                RefTypeId = c.RefTypeId,
                RefNo = c.RefNo,
                RefDate = c.RefDate.ToShortDateString(),
                UnitId = c.UnitId,
                TaxCode = c.TaxCode,
                CustomerId = c.CustomerId,
                VendorId = c.VendorId,
                TotalAmountTax = c.TotalAmountTax,
                Trader = c.Trader,
                StockId = c.StockId,
                TotalAmountOc = c.TotalAmountOc,
                JournalMemo = c.JournalMemo,
                DocumentInclude = c.DocumentInclude,
                AccountingObjectType = c.AccountingObjectType,
                IsCalculatePrice = c.IsCalculatePrice,
             //   ItemTransactionDetails = FromDataTransferObjects(details)
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static ItemTransactionDetailModel FromDataTransferObject(ItemTransactionDetailDto c)
        {
            if (c == null) return null;

            return new ItemTransactionDetailModel
                {
                    ItemTransactionDetailId = c.ItemTransactionDetailId,
                    InventoryItemId = c.InventoryItemId,
                    AccountNumber = c.AccountNumber,
                    CorrespondingAccountNumber = c.CorrespondingAccountNumber,
                    Description = c.Description,
                    AmountOc = c.AmountOc,
                    AmountVat = c.AmountVat,
                    AccountNumberVat = c.AccountNumberVat,
                    TaxRate = c.TaxRate,
                    RefId = c.RefId,
                    Quantity = c.Quantity,
                    Price = c.Price,
                    VoucherTypeId = c.VoucherTypeId,
                };
        }

       

        #endregion

        #region FromDataTransferObjects

        /// <summary>
        /// Froms the data transfer objects.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static IList<ItemTransactionModel> FromDataTransferObjects(ItemTransactionDto[] c)
        {
            return c == null ? null : c.Select(source => FromDataTransferObject((source))).ToList();
        }

        /// <summary>
        /// Froms the data transfer objects.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static IList<ItemTransactionDetailModel> FromDataTransferObjects(IEnumerable<ItemTransactionDetailDto> c)
        {
            return c == null ? null : c.Select(source => FromDataTransferObject((source))).ToList();
        }

        
        #endregion
    }
}
